package com.sike.controller.redis;

import com.sike.entity.R;
import com.sike.service.RandomService2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author vito
 * @version 1.0
 */
@RestController
public class RandomController2 {
    private static final Logger log = LoggerFactory.getLogger(RandomController2.class);
    @Autowired
    private RandomService2 randomService2;
    @Autowired
    private RedisTemplate redisTemplate;

    /*
      抽奖

      @param num 是代表获取奖品的人数，比如1等奖多少人 2等奖多少人。。。。
     * @return
     */
    @GetMapping("/random/paynum")
    public R randomData(Integer num) {
        try {
            String result = null;
            // 1: 随机从集合中获取指定num数量的元素出来，并删除
            List<Integer> persons = this.redisTemplate.opsForSet().pop(RandomService2.RANDOM_SET_KEY, num);
            return R.ok().data("persons", persons).message("抽到的员工有：" + persons);
        } catch (Exception e) {
            log.error("抽奖失败", e);
            return R.error().message("服务忙!!!");
        }
    }

}
